import { Component, OnInit } from '@angular/core';
import { HeroService } from '../../../services/hero.service';
import { StorageService } from '../../../services/storage.service';
import { NzMessageService,NzModalService } from 'ng-zorro-antd';

@Component({
  selector: 'app-userbill',
  templateUrl: './userbill.component.html',
  styleUrls: ['./userbill.component.css']
})
export class UserbillComponent implements OnInit {

  loading = true;
  btn_loading=false;
  public id:number;
  public billId:string='';
  public companyName:string='';
  public yard:string='';
  public isPay:string='';
  public userinfo:string;
  public yardinfo:string;
  public jiaIsVisible:boolean;
  public jianIsVisible:boolean;
  public jiabcode:number;
  public jianbcode:number;
  public jiabValue:number;
  public jianbValue:number;
  public sumMoney:number;
  public billlist:Array<any>=[];
  public PageIndex:number=1;
  public PageSize:number=10;
  public PageTotal:number;
  constructor(private heroService: HeroService,
              private message: NzMessageService,
              private modalService: NzModalService,
              private storageService: StorageService) { }

  ngOnInit() {
    this.getBilllist();
  }

  //获取租户账单列表数据
  getBilllist(){
    this.btn_loading=true;
    let url=host+"/main/userBill/list";
    let model="numberLike="+this.billId+"&companyNameVague="+this.companyName+"&acountNameVague="+this.yard+"&isPay="+this.isPay+"&page="+this.PageIndex+"&pageSize="+this.PageSize;
    this.heroService.post(url,model).then(result=>{
        this.btn_loading=false;
        if(result.code==0){
          this.billlist=result.data.pageBean.result;
          this.userinfo=result.data.connectUser;
          this.yardinfo=result.data.acount;
          this.PageIndex=result.data.pageBean.curPage;  
          this.PageTotal=result.data.pageBean.totalRow;
          this.loading=false;
        }else{
          this.modalService.error({
            nzTitle: '提示',
            nzContent: result.msg
          });
        }
    })
  }

  //已支付
  bePay(id){
    let url=host+"/main/userBill/pay";
    let model="id="+id;
    this.heroService.post(url,model).then(result=>{
        if(result.code==0){
          this.message.success('修改状态成功');
          this.getBilllist();
        }else{
          this.modalService.error({
            nzTitle: '提示',
            nzContent: result.msg
          });
        }
    })
  }
  //加减币
  jiab(id){
    this.jiaIsVisible = true;
    this.id=id;
  }
  jianb(id,sumMoney){
    this.jianIsVisible = true;
    this.sumMoney = sumMoney;
    this.id=id;
  }
  jiaHandleOk(): void {
    if(this.jiabValue){
      let url=host+"/main/userBill/dispatch";
      let model='id='+this.id+'&dispatchMoney='+this.jiabValue;
      this.heroService.post(url,model).then(result=>{
          if(result.code==0){
            this.message.success('加币成功');
            this.jiaIsVisible = false;
            this.getBilllist();
          }else{
            this.modalService.error({
              nzTitle: '提示',
              nzContent: result.msg
            });
          }
      })
    }else{
      this.jiabcode=-1;
    }
  }
  jianHandleOk(): void {
    if(this.jianbValue){
      if(this.jianbValue >this.sumMoney){
        this.modalService.error({
          nzTitle: '提示',
          nzContent: '减币金额不能大于账单总金额'
        });
      }else{
        let url=host+"/main/userBill/dispatch";
        let model='id='+this.id+'&dispatchMoney='+(-this.jianbValue);
        this.heroService.post(url,model).then(result=>{
            if(result.code==0){
              this.message.success('减币成功');
              this.jianIsVisible = false;
              this.getBilllist();
            }else{
              this.modalService.error({
                nzTitle: '提示',
                nzContent: result.msg
              });
            }
        })
      }
    }else{
      this.jianbcode=-1;
    }
  }
  jiaFocus(){
    this.jiabcode=0;
  }
  jianFocus(){
    this.jianbcode=0;
  }
  jiaHandleCancel(): void {
    this.jiaIsVisible = false;
  }
  jianHandleCancel(): void {
    this.jianIsVisible = false;
  }
  //分页
  PageIndexChange(event){
    this.PageIndex=event;
    this.getBilllist();
  }

  //导出Excel
  exportOut() {
    const token = this.storageService.getItem("token");
    window.location.href = host + "/main/printerRecord/excelExport?token="+token+"&numberLike="+this.billId+"&companyNameVague="+this.companyName+"&acountNameVague="+this.yard+"&isPay="+this.isPay;
  }
}

